package cn.com.gridinfo.sooc.core.my.personal.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jeremy.arad.Arad;
import com.jeremy.arad.utils.StringUtils;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.common.LocalMark;
import cn.com.gridinfo.sooc.core.my.dao.MyDao;
import cn.com.gridinfo.sooc.utils.MessageUtils;
import cn.com.gridinfo.sooc.utils.widget.ClearEditText;

/**
 * 更改绑定邮箱
 * Created by 刘晓晨 on 2015/12/18.
 */
public class ChangeEmailActivity extends BaseSoocActivity {

    @InjectView(R.id.btn_send)//（更换，未绑定）提交 按钮
            Button btnSend;
    @InjectView(R.id.changeEmail)//（更换，未绑定）界面
            LinearLayout changeEmail;
    MyDao dao;
    @InjectView(R.id.edit_email)// （更换，未绑定）新的邮箱地址
    ClearEditText editEmail;
    @InjectView(R.id.edit_pwd)//（更换，未绑定）用户密码
    ClearEditText editPwd;
    private  String title;//用来设置本页的标题
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_change_email);
        ButterKnife.inject(this);
        if (StringUtils.isNull(getIntent().getStringExtra("noBoundEmail"))){
            //如果为空，说明绑定了邮箱
            title="更换邮箱";
        }else {
            title="绑定邮箱";
        }
        dao = new MyDao(this);
        btnSend.setEnabled(false);//设置按钮不能点击时候的状态
        btnSend.setBackgroundResource(R.color.color_d3d3);
        editPwd.setImeOptions(EditorInfo.IME_ACTION_DONE);
        editPwd.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if(actionId == EditorInfo.IME_ACTION_DONE){
                    if(validateNotNull()){
                        setBtnSend();
                    }
                }
                return false;
            }
        });
    }
    private boolean validateNotNull(){
        if(StringUtils.isNull(editEmail.getText().toString().trim())){
            editEmail.requestFocus();
            return false;
        }

        if(StringUtils.isNull(editPwd.getText().toString().trim())){
            editPwd.requestFocus();
            return false;
        }
        return true;
    }

    private String newEmail;

    //点击修改按钮
    @OnClick(R.id.btn_send)
    void setBtnSend() {
        newEmail = editEmail.getText().toString().trim();
        String password = editPwd.getText().toString().trim();
        if(!StringUtils.isEmailFormat(newEmail)){
            MessageUtils.showImageShortToast(this,"您输入的邮箱不合法");
        }else {
            dao.onBound(null, -2, newEmail, password);
        }
    }

    //设置标题
    @Override
    public String setupToolBarTitle() {
        return title;
    }

    //设置左边按钮
    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    //返回
    @OnClick(R.id.toolbar_leftbtn)
    void onGoBack() {
        finish();
    }

    //请求成功
    @Override
    public void onRequestSuccess(int requestCode) {
       try {

           if (dao.status.equals("0")) {
               Arad.preferences.putString(LocalMark.USER_EMAIL.getName(), newEmail);//新的存本地
               Arad.preferences.flush();
               if (OnBoundEmailActivity.instance != null) {
                   OnBoundEmailActivity.instance.finish();
               }
               finish();
           }
           MessageUtils.showImageShortToast(this, dao.statusCode);
       }catch (Exception e){
           MessageUtils.showImageShortToast(this,"数据请求异常");
       }
    }

    @Override
    public void onRequestFaild(int requestCode, String errorNo, String errorMessage) {
        super.onRequestFaild(requestCode, errorNo, errorMessage);
        MessageUtils.showImageShortToast(this, dao.statusCode);
    }

    //监听收入匡状态，都有数据的时候才可以点击下一步
    @OnTextChanged(value = {R.id.edit_email, R.id.edit_pwd}, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    void onAfterTextChanged() {
        if (StringUtils.isNull(editEmail.getText().toString()) ||
                StringUtils.isNull(editPwd.getText().toString())) {
            btnSend.setBackgroundResource(R.color.color_d3d3);
            btnSend.setEnabled(false);
        } else {
            btnSend.setBackgroundResource(R.drawable.button_blue_bg);
            btnSend.setEnabled(true);
        }
    }
}
